<template>
  <div>
    <div>
      <el-select v-model="frequency" @change="handleChange">
        <el-option
          v-for="item in statics.frequencies"
          :key="item.value"
          :label="item.value"
          :value="item.value"
          :disabled="item.value == '其他'"
        >
          <span style="float: left">{{ item.value }}</span>
          <span
            v-show="item.seat != ''"
            style="float: right; color: #8492a6; font-size: 10px"
            >{{ item.seat }}</span
          >
        </el-option>
      </el-select>
    </div>
    <div style="text-align: justify; margin-top: 12px">
      <el-tag type="info" style="width: 100%; margin-bottom: 6px"
        >设置{{ target }}的频率为{{ frequency }}</el-tag
      >
      <el-tag
        v-show="defaultSeat != ''"
        type="warning"
        style="width: 100%; margin-bottom: 6px"
      >
        {{ frequency }}为{{ defaultSeat }}的默认频率</el-tag
      >
      <el-tag
        v-show="occupationSeat != ''"
        type="danger"
        style="width: 100%; margin-bottom: 6px"
        >{{ frequency }}正在被{{ occupationSeat }}使用</el-tag
      >
    </div>
    <div style="text-align: center; margin-top: 12px">
      <el-button
        v-if="occupationSeat == ''"
        type="primary"
        @click="handleUpdate"
        >确定
      </el-button>
      <el-button type="primary" link @click="$emit('close')">取消</el-button>
    </div>
  </div>
</template>

<script>
import setSeatAttrib from "@/views/SeatStatus/common/setSeatAttrib.js";
import statics from "./statics.json";
export default {
  props: ["target", "seatStatus"],
  name: "",
  data() {
    return {
      frequency: "124.85",
      defaultSeat: "",
      occupationSeat: "",
      statics,
    };
  },
  methods: {
    handleChange() {
      this.defaultSeat = "";
      for (let i = 0; i < this.statics.frequencies.length; i++) {
        if (
          this.statics.frequencies[i].value + 0 == this.frequency + 0 &&
          this.statics.frequencies[i].seat != this.target
        ) {
          this.defaultSeat = this.statics.frequencies[i].seat;
          break;
        }
      }
      this.occupationSeat = "";
      for (let i = 0; i < this.seatStatus.length; i++) {
        if (
          Number(this.seatStatus[i].frequency) == Number(this.frequency) &&
          this.seatStatus[i].seatName != this.target &&
          (this.seatStatus[i].users.length > 0 ||
            (this.seatStatus[i].sectorCodes > 0 &&
              this.seatStatus[i].sectorCodes[0].length > 0))
        ) {
          this.occupationSeat = this.seatStatus[i].seatName;
          break;
        }
      }
    },
    handleUpdate() {
      setSeatAttrib(this.target, "frequency", this.frequency)
        .then((response) => {
          console.log(response);
          this.$notify.success("已保存频率设置");
          this.$emit("updated");
          this.$emit("close");
        })
        .catch((error) => {
          console.error(error);
          this.$notify.danger(JSON.stringify(error, null, 2));
          this.$emit("close");
        });
      return null;
    },
  },
  mounted() {
    for (let i = 0; i < this.seatStatus.length; i++) {
      if (this.seatStatus[i].seatName == this.target) {
        this.frequency = this.seatStatus[i].frequency;
        this.handleChange();
      }
    }
  },
};
</script>

<style></style>
